
gcc_version := 4.6.3

origin_src_tarball := gcc-core-$(gcc_version).tar.bz2
origin_src_dir := gcc-$(gcc_version)
repo := http://ftpmirror.gnu.org/gcc/gcc-$(gcc_version)/$(origin_src_tarball)

MPFR=mpfr-2.4.2
GMP=gmp-4.3.2
MPC=mpc-0.8.1
prerequisites_tarball := $(MPFR).tar.bz2 $(GMP).tar.bz2 $(MPC).tar.gz
#prerequisites_dirname := $(foreach one, $(prerequisites_tarball), $(patsubst %.tar.%,,$(one)))
prerequisites_dirname := $(basename $(basename $(prerequisites_tarball)))
prerequisites := sources/mpfr sources/gmp sources/mpc
prerequisites_repo := ftp://gcc.gnu.org/pub/gcc/infrastructure

all: 
	@echo NOTHING

build: $(BUILD_DIR)/Makefile
	@($(MAKE) $(MFLAGS) -C $(BUILD_DIR))

$(BUILD_DIR)/Makefile: init
	@if [ ! -d $(BUILD_DIR) ]; then mkdir $(BUILD_DIR); fi
	@cd $(BUILD_DIR) && \
	../sources/configure --target=$(GCC_TOOLCHAIN_TARGET) --program-prefix=$(GCC_TOOLCHAIN_PROGRAM_PREFIX) --enable-languages=c --prefix=$(INSTALL_DIR)

.PHONY:init
init: sources $(prerequisites)
	@echo gcc init done

sources: $(origin_src_tarball)
	@echo extracting files...
	@tar xmf $(origin_src_tarball) && \
	mv $(origin_src_dir) sources
	@echo extract files done

$(origin_src_tarball):
	@wget -c -O $(origin_src_tarball) $(repo);

$(prerequisites): sources $(prerequisites_tarball)
	@tar xmf $(filter $(subst sources/,,$@)%, $(prerequisites_tarball)) -C sources && \
	ln -sf $(filter $(subst sources/,,$@)%, $(prerequisites_dirname)) $@
$(prerequisites_tarball):
	@wget -c -O $@ $(prerequisites_repo)/$@;

patch:
# NOT NEEDED. patch at mspgcc Makefile

unpatch:
# NOT NEEDED. unpatch at mspgcc Makefile

clean:
	if [ -f $(BUILD_DIR)/Makefile ]; then $(MAKE) $(MFLAGS) -C $(BUILD_DIR); fi

distclean:
	@$(RM) -r sources $(BUILD_DIR)

distdistclean: distclean
	@$(RM) $(origin_src_tarball)

install:
	@($(MAKE) $(MFLAGS) -C $(BUILD_DIR) install)

uninstall:
	# TODO:
